package com.ate.lib_common;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ate
 * @description:
 * @date :2020/12/11 11:12
 */
public class BottomBar extends LinearLayout {
    private Builder builder;
    private List<TabBean> list = new ArrayList<>();
    private Context context;
    private int barHeight = 150;
    public BottomBar(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public BottomBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init() {
        if (builder == null) {
            create();
            this.setMinimumHeight(LayoutParams.MATCH_PARENT);
            this.setMinimumWidth(LayoutParams.MATCH_PARENT);

        }
    }

    public void initView() {
        for (TabBean tabBean : list) {
            LinearLayout linearLayout = addView(tabBean);
            this.addView(linearLayout);
        }
    }

    public LinearLayout addView(TabBean tabBean) {
        final LinearLayout linearLayout = new LinearLayout(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,barHeight);;
        params.weight = 1;
        params.gravity = 0;
        linearLayout.setLayoutParams(params);
        final CheckBox checkBox = new CheckBox(context);
        checkBox.setVisibility(INVISIBLE);
        final ImageView sel = new ImageView(context);
        final ImageView unsel = new ImageView(context);
        sel.setImageResource(tabBean.sel);
        unsel.setImageResource(tabBean.unsel);
        linearLayout.addView(unsel);
        linearLayout.addView(sel);
        sel.setVisibility(GONE);
        linearLayout.addView(checkBox);
        linearLayout.setHorizontalGravity(VERTICAL);
        linearLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBox.isChecked()) {
                    checkBox.setChecked(false);
                    unsel.setVisibility(VISIBLE);
                    sel.setVisibility(GONE);
                } else {
                    checkBox.setChecked(true);
                    unsel.setVisibility(GONE);
                    sel.setVisibility(VISIBLE);
                }
            }
        });


        linearLayout.setMinimumWidth(LayoutParams.MATCH_PARENT);
        linearLayout.setMinimumHeight(LayoutParams.WRAP_CONTENT);
        return linearLayout;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public Builder create() {
        if (builder == null) {
            builder = new Builder();
        }
        return builder;
    }


    public class Builder {

        public Builder() {

        }

        public Builder addTab(TabBean tabBean) {
            list.add(tabBean);
            return builder;
        }

        public void build() {
            initView();
        }
    }

    public static class TabBean {
        private int sel;
        private int unsel;
        private String title;

        public TabBean(int sel, int unsel, String title) {
            this.sel = sel;
            this.unsel = unsel;
            this.title = title;
        }
    }
}
